﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace YZQ_DotNetArx.CAD
{
    public static class PolylineToos
    {
        /// <summary>
        /// 通过三维点集创建多段线
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="pts">多段线的顶点</param>
        public static void CreatePolyline(this Polyline pline,Point3dCollection pts)
        {
            for (int i = 0; i < pts.Count; i++)
            {
                //添加多段线的顶点
                pline.AddVertexAt(i,new Point2d(pts[i].X, pts[i].Y),0,0,0);
            }
        }
        /// <summary>
        /// 通过不固定的三维点创建多线段
        /// </summary>
        /// <param name="pline">多线段对象</param>
        /// <param name="pts">多线段的顶点</param>
        public static void CreatePolyline(this Polyline pline,params Point3d[] pts)
        {
            pline.CreatePolyline(new Point3dCollection(pts));
        }
 
        /// <summary>
        /// 通过二维点集合创建多段线
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="pts">多段线的顶点</param>
        public static void CreatePolyline(this Polyline pline,Point2dCollection pts)
        {
            for (int i = 0; i < pts.Count; i++)
            {
                //添加多段线顶点
                pline.AddVertexAt(i, pts[i],0,0,0);
            }
        }
        /// <summary>
        /// 通过不固定的二维点创建多段线
        /// </summary>
        /// <param name="pline">对短线对象</param>
        /// <param name="pts">多段线的顶点</param>
        public static void CreatePolyline(this Polyline pline, params Point2d[] pts)
        {
            pline.CreatePolyline(new Point2dCollection(pts));
        }
        /// <summary>
        /// 创建正多边形
        /// </summary>
        /// <param name="pline">多线段对象</param>
        /// <param name="centerPoint">多边形中心点</param>
        /// <param name="number">边数</param>
        /// <param name="radius">外接圆半径</param>
        public static void CreatePolygon(this Polyline pline,Point2d centerPoint,int number,double radius)
        {
            Point2dCollection pts= new Point2dCollection();
            double angle = 2 * Math.PI / number; //计算每条边对应的角度
            //计算多边形的顶点
            for (int i = 0; i < number; i++)
            {
                Point2d pt = new Point2d(centerPoint.X+radius*Math.Cos(i*angle),centerPoint.Y+radius*Math.Sin(i*angle));
                pts.Add(pt);
            }
            pline.CreatePolyline(pts);
            pline.Closed = true;
        }
    }
}
